package cn.pconline.officialclub.entity;

import cn.pconline.autoclub.util.EncodeUtils;
import cn.pconline.autoclub.util.WebUtils;
import com.alibaba.fastjson.JSONObject;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.gelivable.dao.Column;
import org.gelivable.dao.Entity;
import org.gelivable.dao.Id;
import org.gelivable.dao.Transient;

/**
 * 官方车友会活动
 *
 * @author <a href="mailto:chensy0203@163.com">chensy</a>
 */
@Entity(tableName = "oac_activity")
public class OActivity extends EntityKit implements EntityWithClub{

    public static final int STATUS_NO = 0;
    public static final int STATUS_YES = 1;
    public static final int HAS_LONGCONTENT = 1;
    public static final int NOTHAS_LONGCONTENT = 0;
    public static final int CONTENT_LENGTH = 255;
    /**
     * 线上活动
     */
    public static final int IS_ONLINE = 1;
    /**
     * 线下活动
     */
    public static final int NOT_ONLINE = 0;
    /**
     * 活动ID
     */
    @Id
    @Column(name = "activity_id")
    private long activityId;
    /**
     * 官方车友会ID
     */
    @Column(name = "o_club_id")
    private long oClubId;
    /**
     * 标题
     */
    @Column(name = "title")
    private String title;
    /**
     * 内容
     */
    @Column(name = "content")
    private String content;
    /**
     * 内容是否超出255 0--<=255 1-->255
     */
    @Column(name = "long_content")
    private int longContent;
    /**
     * 活动开始时间
     */
    @Column(name = "begin_date")
    private Date beginDate;
    /**
     * 活动结束时间
     */
    @Column(name = "end_date")
    private Date endDate;
    /**
     * 报名截止时间
     */
    @Column(name = "signup_date")
    private Date signupDate;
    /**
     * 是否线上活动 1--线上 0--线下
     */
    @Column(name = "online_status")
    private int onlineStatus;
    /**
     * 省
     */
    @Column(name = "province_id")
    private long provinceId;
    /**
     * 省name
     */
    @Column(name = "province_name")
    private String provinceName;
    /**
     * 城市
     */
    @Column(name = "city_id")
    private long cityId;
    /**
     * 城市name
     */
    @Column(name = "city_name")
    private String cityName;
    /**
     * 活动经费
     */
    @Column(name = "money")
    private int money;
    /**
     * 标题图链接url，要保留原图URl
     */
    @Column(name = "pic_url")
    private String picUrl;
    /**
     * 参加人数
     */
    @Column(name = "join_num")
    private int joinNum;
    /**
     * 活动报名上限
     */
    @Column(name = "max_join")
    private int maxJoin;
    /**
     * 活动发起人
     */
    @Column(name = "create_by")
    private long createBy;
    /**
     * 创建时间
     */
    @Column(name = "create_at")
    private Date createAt;
    /**
     * 活动状态
     */
    @Column(name = "status")
    private int status;
    /**
     * 活动状态显示code
     */
    @Transient
    public String userActivityStatus;
    /**
     * 活动状态显示msg
     */
    @Transient
    public String userActivityStatusMsg;
    
    /**
     * 所属品牌车友会
     */
    @Transient
    public OClub oClub;
    /**
     * 完整内容
     */
    @Transient(cache = false)
    private String fullContent;
    
	public long getActivityId() {
        return activityId;
    }

    public long getoClubId() {
        return oClubId;
    }

    public void setoClubId(long oClubId) {
        this.oClubId = oClubId;
    }

    public void setActivityId(long activityId) {
        this.activityId = activityId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getLongContent() {
        return longContent;
    }

    public void setLongContent(int longContent) {
        this.longContent = longContent;
    }
    
    public Date getBeginDate() {
        return beginDate;
    }

    public void setBeginDate(Date beginDate) {
        this.beginDate = beginDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Date getSignupDate() {
        return signupDate;
    }

    public void setSignupDate(Date signupDate) {
        this.signupDate = signupDate;
    }

    public int getOnlineStatus() {
        return onlineStatus;
    }

    public void setOnlineStatus(int onlineStatus) {
        this.onlineStatus = onlineStatus;
    }

    public long getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(long provinceId) {
        this.provinceId = provinceId;
    }

    public String getProvinceName() {
        return provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }

    public long getCityId() {
        return cityId;
    }

    public void setCityId(long cityId) {
        this.cityId = cityId;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public String getPicUrl() {
        return picUrl;
    }

    public void setPicUrl(String picUrl) {
        this.picUrl = picUrl;
    }

    public int getJoinNum() {
        return joinNum;
    }

    public void setJoinNum(int joinNum) {
        this.joinNum = joinNum;
    }

    public long getCreateBy() {
        return createBy;
    }

    public void setCreateBy(long createBy) {
        this.createBy = createBy;
    }

    public Date getCreateAt() {
        return createAt;
    }

    public void setCreateAt(Date createAt) {
        this.createAt = createAt;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    /**
     * 兼容geli
     *
     * @return
     */
    public long getOClubId() {
        return oClubId;
    }

    /**
     * 兼容geli
     *
     * @return
     */
    public void setOClubId(long oClubId) {
        this.oClubId = oClubId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getMaxJoin() {
        return maxJoin;
    }

    public void setMaxJoin(int maxJoin) {
        this.maxJoin = maxJoin;
    }

    public String getFullContent() {
        return fullContent;
    }

    public String getUserActivityStatus() {
        return userActivityStatus;
    }

    public void setUserActivityStatus(String userActivityStatus) {
        this.userActivityStatus = userActivityStatus;
    }

    public String getUserActivityStatusMsg() {
        return userActivityStatusMsg;
    }

    public void setUserActivityStatusMsg(String userActivityStatusMsg) {
        this.userActivityStatusMsg = userActivityStatusMsg;
    }

    public void setFullContent(String fullContent) {
        this.fullContent = fullContent;
    }
     public String getFullContent_660() {
        Pattern pattern = Pattern.compile("\\[img=\\d+,\\d+\\]");
        Matcher matcher = pattern.matcher(fullContent);
        StringBuffer sbr = new StringBuffer();
        while (matcher.find()) {
            String img = matcher.group();
            try {
                int eqIndex = img.indexOf("=");
                int dotIndex = img.indexOf(",");
                int w = Integer.parseInt(img.substring(eqIndex + 1, dotIndex));
                int endIndex = img.indexOf("]");
                int h = Integer.parseInt(img.substring(dotIndex + 1, endIndex));
                if (w > 660) {
                    h = 10000 * h / w * 660 / 10000;
                    w = 660;
                    img = "[img=" + w + "," + h + "]";
                }
            } catch (java.lang.NumberFormatException e) {
                //可能是脏数据，忽略
            }
            matcher.appendReplacement(sbr, img);
        }
        matcher.appendTail(sbr);
        return sbr.toString();
    }
    public String getPicUrl160x120() {
        return convertImage(picUrl, "160x120");
    }
    public String getPicUrl300x200() {
        return convertImage(picUrl, "300x200");
    }
    public JSONObject getJSON(boolean xssEncode){
        JSONObject obj = new JSONObject();
        obj.put("activityId", activityId);
        obj.put("content", EncodeUtils.encodeForHTML(content, xssEncode));
        obj.put("beginDate", beginDate.getTime());
        obj.put("cityId", cityId);
        obj.put("cityName",EncodeUtils.encodeForHTML(cityName, xssEncode));
        obj.put("createAt", createAt.getTime());
        obj.put("createBy", createBy);
        obj.put("endDate", endDate.getTime());
        obj.put("joinNum", joinNum);
        obj.put("maxJoin", maxJoin);
        obj.put("money", money);
        obj.put("oClubId", oClubId);
        obj.put("onlineStatus", onlineStatus);
        obj.put("picUrl",picUrl);
        obj.put("provinceId", provinceId);
        obj.put("provinceName", provinceName);
        obj.put("signupDate", signupDate);
        obj.put("status", status);
        obj.put("clubName", oClub==null?"":EncodeUtils.encodeForHTML(oClub.getName(), xssEncode));
        obj.put("title", EncodeUtils.encodeForHTML(title,xssEncode));
        return obj;
    }
    public JSONObject getJSONFilterUbb(boolean xssEncode){
        JSONObject obj = getJSON(xssEncode);
        obj.put("content", EncodeUtils.encodeForHTML(WebUtils.byteSubstring4Reply(content),xssEncode));
        return obj;
    }
    public OClub getoClub() {
       return oClub;
    }

    public void setoClub(OClub oClub) {
        this.oClub = oClub;
    }
    public String getActivityAddress(){
        if(onlineStatus == IS_ONLINE){
            return "线上";
        }else{
            return provinceName+"-"+cityName;
        }
    }
}
